package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequiresPermissions(value = {"permission:list", "权限查询"},logical = Logical.OR)
    //查询所有
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){

        PageInfo<Permission> result = permissionService.query(qo);
        model.addAttribute("result",result);
        return "/permission/list";
    }


    //删除
    @RequiresPermissions(value = {"permission:delete", "权限删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        permissionService.deleteByPrimaryKey(id);
        return new JsonResult();
    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
        }catch (Exception e){
            return new JsonResult(false,"加载失败");
        }
        return new JsonResult(true,"加载成功");
    }

}
